// 根据 id 和 pid 把路由转换成树形结构
let obj = [
    { "id": 1, "pid": 0, "text": "用户管理", "name": "users", "path": "/users" },
    { "id": 2, "pid": 0, "text": "权限管理", "name": "auth", "path": "/auth" },
    { "id": 3, "pid": 0, "text": "商品管理", "name": "order" },

    { "id": 4, "pid": 1, "text": "用户列表", "name": "usersList", "path": "/users/list" },
    { "id": 5, "pid": 1, "text": "用户详情", "name": "usersDetail", "path": "/users/detail" },
    { "id": 6, "pid": 2, "text": "权限列表", "name": "authList", "path": "/auth/list" },
    { "id": 7, "pid": 2, "text": "权限详情", "name": "authDetail", "path": "/auth/detail" },
    { "id": 8, "pid": 3, "text": "订单列表", "name": "order", "path": "/order/list" },
    { "id": 9, "pid": 3, "text": "订单数据", "name": "orderData", "path": "/order/data" },
    { "id": 10, "pid": 9, "text": "订单数据价格", "name": "orderDataPrice", "path": "/order/data/price" },
    { "id": 11, "pid": 9, "text": "订单数据详情", "name": "orderDataDetail", "path": "/order/data/detail" }
]

function formatRouterTree(obj) {
    let parent = obj.filter(item => item.pid == 0),
        children = obj.filter(item => item.id != 0);
    function dataTree(parent, children) {
        parent.forEach(p => {
            children.forEach((c, i) => {
                if(p.id === c.pid) {
                    if(p.children) {
                        p.children.push(c);
                    }else {
                        p.children = [c];
                    }
                    let _c = JSON.parse(JSON.stringify(children));
                    _c.splice(i, 0);
                    dataTree([c], children);
                }
            })
        })
    }
    dataTree(parent, children);
    return parent;
}



let router = formatRouterTree(obj);
console.log(router);
